package asia.doing.controller.manage;

import asia.doing.common.Result;
import asia.doing.entity.Article;
import asia.doing.service.manage.ArticleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;


@Api(tags = "文章管理" ,description = "后台管理系统文章相关接口")
@RestController(value = "manageArticleController")
@RequestMapping("/v1/manage/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;


    @ApiOperation(value = "id查询文章")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Article article = new Article();
        article.setId(id);
        List<Article> articles = articleService.selectArticle(article);
        return Result.success(articles.get(0));
    }
    @ApiOperation(value = "新增文章")
    @PostMapping
    public Result addArticle(@RequestBody Article article) {
        articleService.addArticle(article);
        return Result.success();
    }

    @ApiOperation(value = "更新文章")
    @PutMapping
    public Result updateArticle(@RequestBody Article article) {
        articleService.updateArticle(article);
        return Result.success();
    }

    @ApiOperation(value = "删除文章")
    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable Integer id) {
        articleService.deleteArticle(id);
        return Result.success();
    }


    /*
     * 分页查询
     */
    /**
     * 查询所有的文章
     * @param article 这里分页要传递文章id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "文章分页")
    @GetMapping
    public Result selectPage(Article article,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "2") Integer pageSize) {
        PageInfo<Article> page = articleService.selectPage(article, pageNum, pageSize);
        return Result.success(page);
    }
}
